package cn.mily.org.service.impl;

import cn.mily.basics.service.impl.BasicServiceImpl;
import cn.mily.basics.utils.MD5Utils;
import cn.mily.basics.utils.StrUtils;
import cn.mily.org.domain.Employee;
import cn.mily.org.domain.Shop;
import cn.mily.org.mapper.EmployeeMapper;
import cn.mily.org.mapper.ShopMapper;
import cn.mily.org.service.IShopService;
import cn.mily.user.domain.LoginInfo;
import cn.mily.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private ShopMapper mapper;

    /**
     * 注册商家
     * @param shop
     */
    @Override
    @Transactional
    public void regShop(Shop shop) {

        super.add(shop);/*父类添加方法*/
        Employee emp = shop.getAdmin();/*获取员工*/
        emp.setShop(shop);/*设置店铺*/
        emp.setSalt(StrUtils.getComplexRandomString(10));
        emp.setPassword(MD5Utils.encrypByMd5(emp.getPassword()+emp.getSalt()));/*密码加密*/
        LoginInfo loginInfo= new LoginInfo();/*登录信息*/
        BeanUtils.copyProperties(emp, loginInfo);
        loginInfoMapper.add(loginInfo);/*登录信息表中添加*/
        emp.setLogininfo_id(loginInfo);
        employeeMapper.add(emp);/*员工表中添加商家管理者 并添加id*/
        super.update(shop);/*管理者有id后更新*/
    }

    @Override
    public void updateState(Shop shop) {
        mapper.updateState(shop);
    }
}
